package com.shop.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.shop.context.BaseContext;
import com.shop.service.SetmealService;
import com.shop.constant.MessageConstant;
import com.shop.constant.StatusConstant;
import com.shop.dto.SetmealDTO;
import com.shop.dto.SetmealPageQueryDTO;
import com.shop.entity.Goods;
import com.shop.entity.Setmeal;
import com.shop.entity.SetmealGoods;
import com.shop.exception.DeletionNotAllowedException;
import com.shop.exception.SetmealEnableFailedException;
import com.shop.mapper.GoodsMapper;
import com.shop.mapper.SetmealGoodsMapper;
import com.shop.mapper.SetmealMapper;
import com.shop.result.PageResult;
import com.shop.vo.GoodsItemVO;
import com.shop.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealGoodsMapper setmealGoodsMapper;
    @Autowired
    private GoodsMapper dishMapper;

    /**
     * 新增套餐  但是要保持套餐和商品之间的关系
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //向套餐表插入数据
        setmealMapper.insert(setmeal);
        //获取生成的套餐id
        Long setmealId = setmeal.getId();

        List<SetmealGoods> setmealGoodses = setmealDTO.getSetmeal();
        setmealGoodses.forEach(setmealGoods -> {
            setmealGoods.setSetmealId(setmealId);
        });
        //保存套餐和商品的关联关系
        setmealGoodsMapper.insertBatch(setmealGoodses);
    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        int pageNum = setmealPageQueryDTO.getPage();
        int pageSize = setmealPageQueryDTO.getPageSize();

        PageHelper.startPage(pageNum, pageSize);
//        System.out.println("当前线程id" + BaseContext.getCurrentId());
//        setmealPageQueryDTO.setCreateUser(BaseContext.getCurrentId());
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @Override
    public void delete(List<Long> ids) {
        ids.forEach(id ->{
            Setmeal setmeal = setmealMapper.getById(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE) {
                //起售中的套餐不能删除
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });
        ids.forEach(setmealId -> {
            //删除套餐表中的数据
            setmealMapper.deleteById(setmealId);
            //删除套餐商品关系表中的数据
            setmealGoodsMapper.deleteBySetmealId(setmealId);
        });
    }

    /**
     * 根据id查询套餐以及对应的商品信息
     * @param id
     * @return
     */
    @Override
    public SetmealVO getByIdWithGoods(Long id) {
      Setmeal setmeal = setmealMapper.getById(id);
      List<SetmealGoods> setmealGoodses = setmealGoodsMapper.getBySetmealId(id);

      SetmealVO setmealVO = new SetmealVO();
      BeanUtils.copyProperties(setmeal,setmealVO);
      setmealVO.setSetmealGoods(setmealGoodses);
      return setmealVO;
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //1、修改套餐表，执行update
        setmealMapper.update(setmeal);
        //套餐id
        Long setmealId = setmeal.getId();
        //2、删除套餐和商品的关联关系，操作setmeal_dish表，执行delete
        setmealGoodsMapper.deleteBySetmealId(setmealId);

        //3、重新插入套餐和商品的关联关系，操作setmeal_dish表，执行inser
        List<SetmealGoods> setmealGoodses = setmealDTO.getSetmeal();
        setmealGoodses.forEach(setmealGoods -> {
            setmealGoods.setSetmealId(setmealId);
        });

        setmealGoodsMapper.insertBatch(setmealGoodses);
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        //起售套餐时，判断套餐内是否有停售商品，有停售商品提示"套餐内包含未启售商品，无法启售"
        if (status == StatusConstant.ENABLE) {
            List<Goods> dishList = dishMapper.getBySetmealId(id);
            if(dishList != null && dishList.size() > 0){
                dishList.forEach(dish -> {
                    if(StatusConstant.DISABLE == dish.getStatus()){
                        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                });
            }
        }
        Setmeal setmeal = Setmeal.builder()
                .status(status)
                .id(id)
                .build();
        setmealMapper.update(setmeal);
    }


    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询商品选项
     * @param id
     * @return
     */
    public List<GoodsItemVO> getGoodsItemById(Long id) {
        return setmealMapper.getGoodsItemBySetmealId(id);
    }
}
